﻿using System.IO;
using System.Web;
using System.Web.Caching;
using System.Web.Services;
using System.Security.Cryptography;

namespace WebService1
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://localhost:2417/Service1.asmx")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {
        private const string CACHE_KEY_SALARIES = "salaries";
        private const string SALARY_XML_FILE_PATH = @"\App_Data\salary_{0}.xml.aes";


        [WebMethod]
        public string HelloWorld()
        {
            RijndaelManaged mobjCryptoService = new RijndaelManaged(); 
            return "Hello World";
        }

        [WebMethod]
        public byte[] GetSalaries(string salaryDate)
        {
            HttpContext ctx = HttpContext.Current;
            // 取得缓存数据
            byte[] salaries = (byte[])ctx.Cache[CACHE_KEY_SALARIES + salaryDate];
            // 缓存中无此数据，则读取文件数据
            if (salaries == null)
            {
                // 读取文件数据
                string appPath = Server.MapPath("");
                string crpptXmlFilePath = appPath + string.Format(SALARY_XML_FILE_PATH, salaryDate);
                // 读取文件字节流
                salaries = File.ReadAllBytes(crpptXmlFilePath);
                
                // 建立缓存数据
                CacheDependency cacheDependency =
                   new CacheDependency(crpptXmlFilePath);
                ctx.Cache.Insert(CACHE_KEY_SALARIES + salaryDate,
                         salaries,
                         cacheDependency,
                         Cache.NoAbsoluteExpiration,
                         Cache.NoSlidingExpiration,
                         CacheItemPriority.Default,
                         null);

            }

            return salaries;
        }
    }
}
